package cn.com.czpb.client.module.personal.fragment;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import cn.com.czpb.client.R;
import cn.com.czpb.client.api.InterfaceJsonfile;
import cn.com.czpb.client.app.BaseFragment;
import cn.com.czpb.client.custorm.RecycleViewDivider;
import cn.com.czpb.client.custorm.smartrefreshalayout.ClassHeader_other;
import cn.com.czpb.client.model.usercenter.NotifyBean;
import cn.com.czpb.client.model.usercenter.NotifyEntity;
import cn.com.czpb.client.module.personal.adapter.NotifyAdapter;
import cn.com.czpb.client.net.Factory;
import cn.com.czpb.client.utils.LogUtils;
import cn.com.czpb.client.utils.ParamUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * Created by Administrator on 2017/8/29.
 */

public class NotifyFragment extends BaseFragment implements OnRefreshListener,BaseQuickAdapter.RequestLoadMoreListener{
    @BindView(R.id.swipe_layout)
    SmartRefreshLayout swipe_layout;
    @BindView(R.id.recycler_message)
    RecyclerView recyclerView;
    NotifyAdapter adapter;
    List<NotifyBean> list;
    int Page = 1;
    int PageSize = 15;
    boolean mFlagRefresh;

    @Override
    public int setMyContentView() {
        return R.layout.fragment_message;
    }

    @Override
    public void initView() {

    }
    public static NotifyFragment newInstance() {
        NotifyFragment notifyfragment = new NotifyFragment();
        return notifyfragment;
    }

    @Override
    public void initData() {
        list = new ArrayList<>();
        adapter = new NotifyAdapter(list);
        LinearLayoutManager manager = new LinearLayoutManager(activity);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(manager);
        recyclerView.setAdapter(adapter);
        initList();
        int height= (int) getResources().getDimension(R.dimen.x2);
        recyclerView.addItemDecoration(new RecycleViewDivider(activity,
                LinearLayoutManager.HORIZONTAL,height,
                getResources().getColor(R.color.color_e8e8e8)));
//        swipe_layout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
//            @Override
//            public void onRefresh() {
//                mFlagRefresh = true;
//                Page = 1;
//                initList();
//            }
//        });

        swipe_layout.setRefreshHeader(new ClassHeader_other(activity));
        swipe_layout.setOnRefreshListener(this );
//        adapter.setOnItemChildClickListener(this);
        adapter.setOnLoadMoreListener(this, recyclerView);
    }

    private void initList() {
        Map<String, String> map = new HashMap<>();

        map.put("siteid", InterfaceJsonfile.SITEID);
        Factory.provideHttpService().my_notity(ParamUtils.getRequestParam(map))
                .onBackpressureBuffer()
                .subscribeOn(Schedulers.io())
                .filter(new Func1<NotifyEntity, Boolean>() {
                    @Override
                    public Boolean call(NotifyEntity notifiEntity) {
                        return true;
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<NotifyEntity>() {
                    @Override
                    public void call(NotifyEntity notifiEntity) {
                        LogUtils.e("code--" + notifiEntity.code);
                        LogUtils.e("code--" + notifiEntity.msg);

                        if (mFlagRefresh) {
                            swipe_layout.finishRefresh();
                        }
                        if ("200".equals(notifiEntity.code)) {

                            setListData(notifiEntity.data);

                            adapter.notifyDataSetChanged();

                            LogUtils.e(list.size() + "");
                        } else if (!mFlagRefresh) {
                            Page--;
                        }

                        mFlagRefresh = false;
                    }

                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        LogUtils.i("throwable--" + throwable.getMessage());
                    }
                });


    }

    private void setListData(List<NotifyBean> list) {
        if (mFlagRefresh) {
            adapter.setNewData(list);
            adapter.notifyDataSetChanged();
        } else {
            adapter.addData(list);
            adapter.notifyDataSetChanged();
            adapter.loadMoreComplete();
        }
        LogUtils.i("size-->" + adapter.getData().size());
        if (list.size() < PageSize) {
            //关闭刷新
            adapter.loadMoreEnd(true);
        }
    }

    @Override
    public void onLoadMoreRequested() {
        Page++;
        initList();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

    }

    @Override
    public void onRefresh(RefreshLayout refreshlayout) {

    }
}
